package teacher;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TeacherDataFetcher {

    public static int getCourseCount(int teacherId) {
        try {
            Connectmysql conn = new Connectmysql(
                    "jdbc:mysql://localhost:3306/exam",
                    "root",
                    "root",
                    "com.mysql.cj.jdbc.Driver"
            );
            if (conn.open()) {
                String sql = "SELECT COUNT(DISTINCT course_id) AS count FROM teaching WHERE teacher_id = ? AND status = 1";
                ResultSet rs = conn.executeSql(sql, new Object[]{teacherId});
                return rs.next() ? rs.getInt("count") : 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static List<Map<String, Object>> getTeacherExams(int teacherId) {
        List<Map<String, Object>> exams = new ArrayList<>();
        try {
            Connectmysql conn = new Connectmysql(
                    "jdbc:mysql://localhost:3306/exam",
                    "root",
                    "root",
                    "com.mysql.cj.jdbc.Driver"
            );
            if (conn.open()) {
                String sql = "SELECT e.*, c.course_name FROM exam e " +
                        "JOIN course c ON e.course_id = c.course_id " +
                        "WHERE e.teacher_id = ? ORDER BY e.apply_time DESC";
                ResultSet rs = conn.executeSql(sql, new Object[]{teacherId});

                while (rs.next()) {
                    Map<String, Object> exam = new HashMap<>();
                    exam.put("exam_id", rs.getInt("exam_id"));
                    exam.put("exam_name", rs.getString("exam_name"));
                    exam.put("course_name", rs.getString("course_name"));
                    exam.put("start_time", rs.getTimestamp("start_time"));
                    exam.put("status", rs.getInt("status"));
                    exams.add(exam);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return exams;
    }

    public static List<Map<String, Object>> getTeachingCourses(int teacherId) {
        List<Map<String, Object>> courses = new ArrayList<>();
        try {
            Connectmysql conn = new Connectmysql(
                    "jdbc:mysql://localhost:3306/exam",
                    "root",
                    "root",
                    "com.mysql.cj.jdbc.Driver"
            );
            if (conn.open()) {
                String sql = "SELECT t.*, c.course_name, cl.class_name " +
                        "FROM teaching t " +
                        "JOIN course c ON t.course_id = c.course_id " +
                        "JOIN class cl ON t.class_id = cl.class_id " +
                        "WHERE t.teacher_id = ? AND t.status = 1";
                ResultSet rs = conn.executeSql(sql, new Object[]{teacherId});

                while (rs.next()) {
                    Map<String, Object> course = new HashMap<>();
                    course.put("course_name", rs.getString("course_name"));
                    course.put("class_name", rs.getString("class_name"));
                    courses.add(course);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return courses;
    }
}